package com.lwj.service.system;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lwj.dto.system.CategoryListDto;
import com.lwj.dto.system.DictionaryCategoryDto;
import com.lwj.exception.BusinessException;
import com.lwj.mapper.system.DictionaryCategoryMapper;
import com.lwj.model.system.DictionaryCategory;
import com.lwj.service.DozerService;
import com.lwj.vo.PageBean;
import com.lwj.vo.system.DictionaryCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DictionaryCategoryServiceImpl implements DictionaryCategoryService {
    @Autowired
    private DictionaryCategoryMapper dictionaryCategoryMapper;
    @Autowired
    private DozerService dozerService;

    @Override
    public int deleteByPrimaryKey(String categoryId) {
        return dictionaryCategoryMapper.deleteByPrimaryKey(categoryId);
    }

    @Override
    public int insertSelective(DictionaryCategory record) {
        return dictionaryCategoryMapper.insertSelective(record);
    }

    @Override
    public DictionaryCategory selectByPrimaryKey(String categoryId) {
        return dictionaryCategoryMapper.selectByPrimaryKey(categoryId);
    }

    @Override
    public int updateByPrimaryKeySelective(DictionaryCategory record) {
        return dictionaryCategoryMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public DictionaryCategoryVo selectByCode(String code) {
        DictionaryCategoryVo dictionaryCategoryVo = dictionaryCategoryMapper.selectByCode(code);
        DictionaryCategory map = dozerService.map(dictionaryCategoryVo, DictionaryCategory.class);
        System.out.println(map);
        return dictionaryCategoryMapper.selectByCode(code);
    }

    @Override
    public PageBean<DictionaryCategoryVo> categoryList(CategoryListDto dto) {
        Page<DictionaryCategoryVo> objects = PageHelper.startPage(dto.getPageNumber(), dto.getPageSize());
        dictionaryCategoryMapper.categoryList(dto);
        return PageBean.page(objects);
    }

    @Override
    @Transactional
    public int updateCategory(DictionaryCategoryDto dto) {
        DictionaryCategory dictionaryCategory = dozerService.map(dto, DictionaryCategory.class);
        dictionaryCategory.setParentCategoryId(null);
        dictionaryCategory.setCategoryCode(null);
        int i = dictionaryCategoryMapper.updateByPrimaryKeySelective(dictionaryCategory);
        if(i!=1){
            throw new BusinessException("保存失败");
        }
        return i;
    }
}
